unit uWybierzTowar;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ADODB, Grids, DBGrids, StdCtrls, Buttons, ExtCtrls;

const
  CP = Char($27);

type
  TFSzukajTowaru = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    RxDBGrid1: TDBGrid;
    QTowar: TADOQuery;
    DataSource1: TDataSource;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Bevel1: TBevel;
    ListBox1: TListBox;
    btDodaj: TButton;
    btUsun: TButton;
    Conn: TADOConnection;
    BitBtn3: TBitBtn;
    procedure Edit1Change(Sender: TObject);
    procedure RxSpeedButton2Click(Sender: TObject);
    procedure RxDBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BitBtn1Click(Sender: TObject);
    procedure RxDBGrid1DblClick(Sender: TObject);
    procedure RxDBGrid1GetCellParams(Sender: TObject; Field: TField;
      AFont: TFont; var Background: TColor; Highlight: Boolean);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure btDodajClick(Sender: TObject);
    procedure btUsunClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Koniec : integer;
  end;


implementation



{$R *.DFM}

procedure TFSzukajTowaru.Edit1Change(Sender: TObject);
begin
  with QTowar do begin
    Close;
    SQL[2] := 'ITEMNMBR LIKE '+CP+'%'+Edit1.Text+'%'+CP;
    SQL[3] := 'and ITEMDESC LIKE '+CP+'%'+Edit2.Text+'%'+CP;
    Open;
  end;
end;



procedure TFSzukajTowaru.RxSpeedButton2Click(Sender: TObject);
begin
   Close;
end;

procedure TFSzukajTowaru.RxDBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Ord(Key)=13) or (Ord(Key)=32) then
    RxDBGrid1DblClick(Sender);
end;

procedure TFSzukajTowaru.BitBtn1Click(Sender: TObject);
begin
  Koniec := 1;
  Close;
end;

procedure TFSzukajTowaru.RxDBGrid1DblClick(Sender: TObject);
var
  i : integer;
  jest : boolean;
  KTM : string;
begin
   if not QTowar.IsEmpty then
   begin
     KTM := Trim(QTowar.FieldByName('ITEMNMBR').AsString);
     jest := false;
     for i := 0 to ListBox1.Items.Count-1 do
       if ListBox1.Items[i]=KTM then
         jest := true;
     if not jest then
       ListBox1.Items.Add(KTM) ;
     if ListBox1.Items.Count>=20 then
       btDodaj.Enabled := False;
   end;
end;

procedure TFSzukajTowaru.RxDBGrid1GetCellParams(Sender: TObject;
  Field: TField; AFont: TFont; var Background: TColor; Highlight: Boolean);
begin
 if (((Sender as TDBGrid).DataSource.DataSet.RecNo mod 2)=0) then
   Background := RGB($C0, $C0, $C0 )
 else
   Background := clWhite;
 if Highlight then
   Background := clYellow;
 AFont.Color := clBlack;
end;

procedure TFSzukajTowaru.FormActivate(Sender: TObject);
begin
  QTowar.Open;
end;

procedure TFSzukajTowaru.BitBtn2Click(Sender: TObject);
begin
  Koniec := 0;
  Close;
end;

procedure TFSzukajTowaru.btDodajClick(Sender: TObject);
var
  i : integer;
  jest : boolean;
  KTM : string;
begin
   if not QTowar.IsEmpty then
   begin
     KTM := Trim(QTowar.FieldByName('ITEMNMBR').AsString);
     jest := false;
     for i := 0 to ListBox1.Items.Count-1 do
       if ListBox1.Items[i]=KTM then
         jest := true;
     if not jest then
       ListBox1.Items.Add(KTM) ;
     if ListBox1.Items.Count>=20 then
       btDodaj.Enabled := False;
   end;
end;

procedure TFSzukajTowaru.btUsunClick(Sender: TObject);
begin
  if ListBox1.Items.Count>0 then
  begin
    ListBox1.Items.Delete(ListBox1.ItemIndex);
    btDodaj.Enabled := True;
  end;
end;

procedure TFSzukajTowaru.FormShow(Sender: TObject);
begin
  Koniec := 0;
end;

procedure TFSzukajTowaru.BitBtn3Click(Sender: TObject);
begin
  Koniec := 2;
  Close;
end;

procedure TFSzukajTowaru.ListBox1DblClick(Sender: TObject);
begin
  if ListBox1.Items.Count>0 then
  begin
    ListBox1.Items.Delete(ListBox1.ItemIndex);
    btDodaj.Enabled := True;
  end;
end;

end.
